package zilvic;

import zilvic.game.Game;
import java.awt.Canvas;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferStrategy;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Display extends Canvas {
    private BufferStrategy strategy;

    private final static int DISPLAY_WIDTH  = 320;
    private final static int DISPLAY_HEIGHT = 320;

    public Display(Game game) {
        JFrame container = new JFrame("Zilvic");
        JPanel panel = (JPanel) container.getContentPane();
        panel.setPreferredSize(new Dimension(DISPLAY_WIDTH, DISPLAY_HEIGHT));
	panel.setLayout(null);
        setBounds(0,0,800,600);
	panel.add(this);
        setIgnoreRepaint(true);
        container.pack();
        container.setResizable(false);
        container.setVisible(true);

        container.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                Game.getInstance().gameEnd();
                System.exit(0);
            }
        });

        addKeyListener(Game.getInputHandler());

        requestFocus();

        createBufferStrategy(2);
	strategy = getBufferStrategy();

        Game.getInstance().gameStart();
    }

    @Override
    public void paint(Graphics g) {
        Game.getInstance().gameDraw(g);
    }
}